package net.wanho.zx.zx_cms.controller;

import com.alibaba.fastjson.JSONObject;
import freemarker.template.TemplateException;
import net.wanho.zx.pojo.cms.CmsPage;
import net.wanho.zx.util.AjaxResult;
import net.wanho.zx.zx_cms.service.CmsPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author： Depeng.Xu
 * @date： 2020/10/26 16:48
 * @description： TODO
 * @version: 1.0
 */

@RestController
public class CmsPageController {
    @Autowired
    private CmsPageService cmsPageService;

    @GetMapping("/cms/page/list/{pageNo}/{pageSize}")
    public Page<CmsPage> query(@PathVariable("pageNo") int pageNo, @PathVariable("pageSize") int pageSize, @RequestParam("siteId") String siteId) {
        return cmsPageService.queryCmsPage(pageNo, pageSize, siteId);
    }

    @PostMapping("/cms/page/add")
    public JSONObject addCmsPage(@RequestBody CmsPage cmsPage) {
        JSONObject jsonObject = new JSONObject();
        try {
            cmsPageService.add(cmsPage);
            jsonObject.put("success", true);
            jsonObject.put("message",cmsPage.getPageId());
        } catch (Exception e) {
            jsonObject.put("success", false);
            return jsonObject;
        }
        return jsonObject;
    }


    @DeleteMapping("/cms/page/del/{cmsPageId}")
    public AjaxResult deleteCmsPage(@PathVariable("cmsPageId") String id) {

        try {
            cmsPageService.deleteCmsPageById(id);
            return new AjaxResult(true);
        } catch (Exception e) {
            return new AjaxResult(false);
        }
    }

    @GetMapping("/cms/page/get/{cmsPageId}")
    public CmsPage getCmsPageById(@PathVariable("cmsPageId") String id) {
        return cmsPageService.queryCmsPageById(id);
    }

    @PutMapping("/cms/page/edit")
    public AjaxResult updateCmsPage(@RequestBody CmsPage cmsPage) {
        try {
            cmsPageService.updateCmsPage(cmsPage);
            return new AjaxResult(true);
        } catch (Exception e) {
            return new AjaxResult(false);
        }
    }


    @PostMapping("/cms/page/generateHtml/{cmsPageId}")
    public void generateHtml(@PathVariable("cmsPageId") String pageId) throws IOException, TemplateException {
        cmsPageService.generateHtml(pageId);

    }

    @GetMapping("/cms/page/preview/{cmsPageId}")
    public void preview(@PathVariable("cmsPageId") String pageId, HttpServletResponse response) throws IOException, TemplateException {
        cmsPageService.preview(pageId, response);

    }

    @PostMapping("/cms/page/postPage/{cmsPageId}")
    public JSONObject upload2Gridfs(@PathVariable("cmsPageId") String pageId) throws IOException, TemplateException {
        JSONObject jsonObject = new JSONObject();
        cmsPageService.upload2Gridfs(pageId);
        jsonObject.put("success", true);
        return jsonObject;

    }
}
